Skip to content

Implement unstable trait impl #140399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open

Implement unstable trait impl #140399

wants to merge 8 commits into from

Conversation

tiif
Copy link
Member

@tiif tiif commented Apr 28, 2025

This PR allows marking impls of stable trait with stable type as unstable.

Approach

In std/core, an impl can be marked as unstable by annotating it with #[unstable_feature_bound(feat_name)]. This will add a ClauseKind::Unstable_Feature(feat_name) to the list of predicates in predicates_of .

When an unstable impl's function is called, we will first iterate through all the goals in param_env to check if there is any ClauseKind::UnstableFeature(feat_name) in param_env.

The existence of ClauseKind::Unstable_Feature(feat_name) in param_env means an#[unstable_feature_bound(feat_name)] is present at the call site of the function, so we allow the check to succeed in this case.

If ClauseKind::UnstableFeature(feat_name) does not exist in param_env, we will still allow the check to succeed for either of the cases below:

  1. The feature is enabled through #[feature(feat_name)] outside of std / core.
  2. We are in codegen because we may be monomorphizing a body from an upstream crate which had an unstable feature enabled that the downstream crate do not.

For the rest of the case, it will fail with ambiguity.

Limitation

In this PR, we do not support:

  1. using items that need #[unstable_feature_bound] within stable APIs
  2. annotate main function with #[unstable_feature_bound]
  3. annotate #[unstable_feature_bound] on items other than free function and impl

Acknowledgement

The design and mentoring are done by @BoxyUwU

@rustbot rustbot added A-attributes Area: Attributes (`#[…]`, `#![…]`) S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver) labels Apr 28, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@BoxyUwU BoxyUwU self-assigned this May 27, 2025
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Collaborator

bors commented Jun 10, 2025

☔ The latest upstream changes (presumably #142299) made this pull request unmergeable. Please resolve the merge conflicts.

@rust-bors
Copy link

rust-bors bot commented Jul 14, 2025

☀️ Try build successful (CI)
Build commit: 93970f9 (93970f9d9c3e81fa0797ad083455b0a1f5e54c54, parent: 64b185eddaa1d7ddf5e0c024be23b9cbba6c1722)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (93970f9): comparison URL.

Overall result: ❌ regressions - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.3% [0.1%, 0.8%] 62
Regressions ❌
(secondary)
0.4% [0.1%, 1.3%] 61
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.1%] 1
All ❌✅ (primary) 0.3% [0.1%, 0.8%] 62

Max RSS (memory usage)

Results (primary -4.9%, secondary 2.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.8% [2.4%, 3.1%] 2
Improvements ✅
(primary)
-4.9% [-4.9%, -4.9%] 1
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) -4.9% [-4.9%, -4.9%] 1

Cycles

Results (primary 2.2%, secondary 3.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
2.2% [2.2%, 2.2%] 2
Regressions ❌
(secondary)
3.0% [3.0%, 3.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 2.2% [2.2%, 2.2%] 2

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 463.69s -> 465.733s (0.44%)
Artifact size: 374.63 MiB -> 374.51 MiB (-0.03%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Jul 14, 2025
@tiif
Copy link
Member Author

tiif commented Jul 14, 2025

The only difference between the latest commit and the one being used for perf is comment.

Hi @compiler-errors, I think you might be interested to see the perf result.

@bors
Copy link
Collaborator

bors commented Jul 14, 2025

☔ The latest upstream changes (presumably #143934) made this pull request unmergeable. Please resolve the merge conflicts.

@rustbot
Copy link
Collaborator

rustbot commented Jul 15, 2025

This PR changes rustc_public

cc @oli-obk, @celinval, @ouz-a

rust-bors bot added a commit that referenced this pull request Jul 15, 2025
[Perf] Remove call to hir_attrs in predicates_of

<!-- homu-ignore:start -->
<!--
If this PR is related to an unstable feature or an otherwise tracked effort,
please link to the relevant tracking issue here. If you don't know of a related
tracking issue or there are none, feel free to ignore this.

This PR will get automatically assigned to a reviewer. In case you would like
a specific user to review your work, you can assign it to them by using

    r? <reviewer name>
-->
<!-- homu-ignore:end -->

Test how much regression we still have if we remove the call to ``hir_attrs`` in ``predicates_of`` for #140399

r? ghost
@lcnr
Copy link
Contributor

lcnr commented Jul 16, 2025

looks like the cost is from just the new PredicateKind 🤔

While I think changing Predicate and Clause to be like GenericArg (as in, a tagged pointer and storing different predicate/clause kinds in separate interned list), would likely improve perf and reduce the impact of adding new variants, I don't think we should block this PR on that.

Unless you want to try something to reduce the perf impact, r=me

@BoxyUwU
Copy link
Member

BoxyUwU commented Jul 16, 2025

@bors r=lcnr rollup=never

I would really like to get this perf back but also supporting stability of impls seems quite important so I would prefer to land this then experiment with perf changes afterwards 🤔

@bors
Copy link
Collaborator

bors commented Jul 16, 2025

📌 Commit e331de1 has been approved by lcnr

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jul 16, 2025
@BoxyUwU
Copy link
Member

BoxyUwU commented Jul 16, 2025

@bors r=lcnr,BoxyUwU

@bors
Copy link
Collaborator

bors commented Jul 16, 2025

💡 This pull request was already approved, no need to approve it again.

@bors
Copy link
Collaborator

bors commented Jul 16, 2025

📌 Commit e331de1 has been approved by lcnr,BoxyUwU

It is now in the queue for this repository.

@tiif
Copy link
Member Author

tiif commented Jul 16, 2025

I am happy to send a follow-up PR to try more stuff to claw back the perf

Thanks for all the reviews ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-attributes Area: Attributes (`#[…]`, `#![…]`) perf-regression Performance regression. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. WG-trait-system-refactor The Rustc Trait System Refactor Initiative (-Znext-solver)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants